package com.ren.bottomnavigationview.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.ren.bottomnavigationview.R;
import com.ren.bottomnavigationview.adapter.PagerAdapter;
import com.ren.bottomnavigationview.fragment.BooksFragment;
import com.ren.bottomnavigationview.fragment.DiscoverFragment;
import com.ren.bottomnavigationview.fragment.MoreFragment;
import com.ren.bottomnavigationview.fragment.NewsFragment;
import com.ren.bottomnavigationview.view.BottomNavigationViewHelper;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private ViewPager viewPager;
    private BottomNavigationView bottomNavigationView;
    private List<Fragment> fragmentList = new ArrayList<>();
    private PagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_view);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
        initData();
        initListener();


    }

    private void initData() {
        fragmentList.add(new NewsFragment());
        fragmentList.add(new BooksFragment());
        fragmentList.add(new DiscoverFragment());
        fragmentList.add(new MoreFragment());
        pagerAdapter = new PagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(pagerAdapter);

        viewPager.setCurrentItem(1);//viewPager默认显示第二页
        bottomNavigationView.getMenu().getItem(1).setChecked(true);//底部按钮默认选中第二个
    }

    private void initListener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.item_news:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.item_lib:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.item_find:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.item_more:
                        viewPager.setCurrentItem(3);
                        break;
                }
                return false;
            }
        });
    }
}
